﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.IO.Compression;
using System.Security.Permissions;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using QT.Windows.Shell;

namespace BaiDuSitemap
{
    public partial class Sitemap : GlassForm
    {
        #region Attributes

        Edit.ucSitemap mySitemap = null;
        Edit.ucSnapShot mySnapShot = null;
        Setting.ucGroup myGroup = null;
        Setting.ucURL myUrl = null;
        DataBase.ucDataBase myDBM = null;
        DataBase.ucAdmin myAdmin = null;

        #endregion

        #region Form

        public Sitemap()
        {
            InitializeComponent();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            bool b = true;

            if (this.pMain.Contains(mySitemap) && !mySitemap.Closing()) b = false;
            if (this.pMain.Contains(mySnapShot) && !mySnapShot.Closing()) b = false;
            if (this.pMain.Contains(myGroup) && !myGroup.Closing()) b = false;
            if (this.pMain.Contains(myUrl) && !myUrl.Closing()) b = false;
            if (this.pMain.Contains(myDBM) && !myDBM.Closing()) b = false;

            if (!b)
            {
                MessageBox.Show("有子窗口正在运行，请先关闭相关操作");
                return;
            }

            e.Cancel = false;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            this.menuStrip1.BackColor = AeroColor;
        }

        #endregion

        #region Event

        #region ToolStripMenuItem

        #region File

        private void ToolStripMenuItem_Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

        #region Edit

        private void ToolStripMenuItem_CreateSitemap_Click(object sender, EventArgs e)
        {
            if (this.pMain.Contains(mySitemap))
            {
                mySitemap.BringToFront();
                return;
            }

            mySitemap = new Edit.ucSitemap();
            mySitemap.Location = new Point(0, 0);
            mySitemap.Dock = DockStyle.Fill;
            this.pMain.Controls.Add(mySitemap);
            mySitemap.BringToFront();
        }

        private void ToolStripMenuItem_CheckSnap_Click(object sender, EventArgs e)
        {
            if (this.pMain.Contains(mySnapShot))
            {
                mySnapShot.BringToFront();
                return;
            }

            mySnapShot = new Edit.ucSnapShot();
            mySnapShot.Location = new Point(0, 0);
            mySnapShot.Dock = DockStyle.Fill;
            this.pMain.Controls.Add(mySnapShot);
            mySnapShot.BringToFront();
        }
        
        #endregion

        #region Setting

        private void ToolStripMenuItem_Group_Click(object sender, EventArgs e)
        {
            if (this.pMain.Contains(myGroup))
            {
                myGroup.BringToFront();
                return;
            }

            myGroup = new Setting.ucGroup();
            myGroup.Location = new Point(0, 0);
            myGroup.Dock = DockStyle.Fill;
            this.pMain.Controls.Add(myGroup);
            myGroup.BringToFront();
        }

        private void ToolStripMenuItem_URL_Click(object sender, EventArgs e)
        {
            if (this.pMain.Contains(myUrl))
            {
                myUrl.BringToFront();
                return;
            }

            myUrl = new Setting.ucURL();
            myUrl.Location = new Point(0, 0);
            myUrl.Dock = DockStyle.Fill;
            this.pMain.Controls.Add(myUrl);
            myUrl.BringToFront();
        }

        private void ToolStripMenuItem_Option_Click(object sender, EventArgs e)
        {
            Setting.Option myOption = new Setting.Option();
            try
            {
                myOption.ShowDialog();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                myOption.Dispose();
            }
        }

        #endregion

        #region Help

        private void ToolStripMenuItem_DBManage_Click(object sender, EventArgs e)
        {
            if (this.pMain.Contains(myDBM))
            {
                myDBM.BringToFront();
                return;
            }

            myDBM = new DataBase.ucDataBase();
            myDBM.Location = new Point(0, 0);
            myDBM.Dock = DockStyle.Fill;
            this.pMain.Controls.Add(myDBM);
            myDBM.BringToFront();
        }

        private void toolStripMenuItem_Admin_Click(object sender, EventArgs e)
        {
            if (this.pMain.Contains(myAdmin))
            {
                myAdmin.BringToFront();
                return;
            }

            myAdmin = new DataBase.ucAdmin();
            myAdmin.Location = new Point(0, 0);
            myAdmin.Dock = DockStyle.Fill;
            this.pMain.Controls.Add(myAdmin);
            myAdmin.BringToFront();
        }

        private void ToolStripMenuItem_About_Click(object sender, EventArgs e)
        {
            MessageBox.Show("站点地图生成&统计工具");
        }

        #endregion

        #endregion

        #endregion
    }   
}
